package cn.signalpha.modules.shop.service;

import cn.signalpha.core.tool.utils.BeanUtil;
import cn.signalpha.modules.com.service.UserService;
import cn.signalpha.modules.com.service.UserWalletService;
import cn.signalpha.modules.shop.entity.Publicity;
import cn.signalpha.modules.shop.excel.PublicityExcel;
import cn.signalpha.modules.shop.mapper.PublicityMapper;
import cn.signalpha.modules.shop.vo.PublicityVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 服务类
 *
 * @author signalpha
 */
@Service
@AllArgsConstructor
public class PublicityService extends ServiceImpl<PublicityMapper, Publicity> {

	private final UserService userService;
	private final UserWalletService userWalletService;

	/**
	 * 自定义分页
	 */
    public IPage<PublicityVO> selectPublicityPage(IPage<PublicityVO> page, Map<String, Object> params) {
        return page.setRecords(baseMapper.selectPublicityPage(page, params));
    }

    /**
	 * 自定义导出
	 */
    public List<PublicityExcel> selectPublicityExcel(Map<String, Object> params) {
        return BeanUtil.copy(baseMapper.selectPublicityPage(null, params), PublicityExcel.class);
    }

	/**
	 * 根据类型查找
	 */
	public List<Publicity> listByType(String type) {
		Date date = new Date();
		return baseMapper.selectList(Wrappers.<Publicity>lambdaQuery()
			.eq(Publicity::getType, type)
			.le(Publicity::getBeginTime, date).ge(Publicity::getEndTime, date)
			.orderByDesc(Publicity::getSort).orderByDesc(Publicity::getId)
		);
	}

	/**
	 * 根据商户和类型查找
	 */
	public List<Publicity> listBySellerIdAndType(Long sellerId, String type) {
		Date date = new Date();
		return baseMapper.selectList(Wrappers.<Publicity>lambdaQuery()
			.eq(Publicity::getSellerId, sellerId).eq(Publicity::getType, type)
			.le(Publicity::getBeginTime, date).ge(Publicity::getEndTime, date)
			.orderByDesc(Publicity::getSort).orderByDesc(Publicity::getId)
		);
	}


}

